package com.example.maple.ccsclient.utils;

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.maple.ccsclient.model.DataModel;
import com.example.maple.ccsclient.model.ISendData;
import com.example.maple.ccsclient.model.PicModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.TypeVariable;
import java.util.List;
import java.util.Map;


/**
 * Created by Admin on 2016/3/11.
 */
public class HttpRequestUtils {
	RequestQueue rQueue;
	Gson mGson = null;
	Context mContext = null;
	public HttpRequestUtils(Context context) {
		mContext = context;
		rQueue = Volley.newRequestQueue(mContext);
		mGson = new Gson();
	}

	//Get方法
	public void getRequestString(String url, final ISendData iSendData) {
		StringRequest req = new StringRequest(Request.Method.GET, url,
				//回调响应
				new Response.Listener<String>() {
					@Override
					public void onResponse(String s) {
						NetWorkUtils.isNetworkConnected(mContext);
						iSendData.sendData(s);
					}
					//出错回调
				}, new Response.ErrorListener() {


			@Override
			public void onErrorResponse(VolleyError volleyError) {
				DataModel<String> error = VolleyErrorHelper.getMessage(volleyError,mContext);
				iSendData.sendData(mGson.toJson(error));
			}

		});
		rQueue.add(req);
	}

	public void postRequestString(String url, final Map<String, String> map, final Map<String, String> headers, final ISendData iSendData) {
		StringRequest req = new StringRequest(Request.Method.POST, url,
				//回调响应
				new Response.Listener<String>() {
					@Override
					public void onResponse(String s) {
						iSendData.sendData(s);
					}
					//出错回调
				}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError volleyError) {

				DataModel<String> error = VolleyErrorHelper.getMessage(volleyError,mContext);
				iSendData.sendData(mGson.toJson(error));
			}
		}) {
			//输入参数
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return map;
			}

			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				if (null != headers) {
					return headers;
				}
				return null;
			}
		};


		rQueue.add(req);
	}


	public void postRequestString(String url, final Map<String, String> map, final ISendData iSendData) {
		StringRequest req = new StringRequest(Request.Method.POST, url,
				//回调响应
				new Response.Listener<String>() {
					@Override
					public void onResponse(String s) {
						iSendData.sendData(s);
					}
					//出错回调
				}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError volleyError) {

				DataModel<String> error = VolleyErrorHelper.getMessage(volleyError,mContext);
				iSendData.sendData(mGson.toJson(error));
			}
		}) {
			//输入参数
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return map;
			}


		};
		rQueue.add(req);
	}
}
